package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FastDFSUtil {
    /*
    * 加载Tracker链接信息
    * */
    static {
        try {
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /****
     * 文件上传
     * @param file : 要上传的文件信息封装->FastDFSFile
     * @return String[]
     *          1:文件上传所存储的组名
     *          2:文件存储路径
     */

    public static String[] upload(FastDFSFile file) throws Exception {
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] =new NameValuePair("author",file.getAuthor());

        TrackerServer trackerServer = getTrackerServer();

        //通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer,null);
        /*
        * 通过StorageClient访问Storage，实现文件上传，并且获取文件上传后的存储信息
        * 1：上传文件的字节数组
        * 2：文件的扩展名，jpg
        * 3：文件的附加参数，拍摄地点，时间
        * */
        /*
        * upload[0]文件上传所存储的Storage组名
        * upload[1]文件存储Storage的文件名字
        * */
        String[] uploads = storageClient.upload_file(file.getContent(),file.getExt(),meta_list);

        return uploads;
    }

    /*
    * 获取文件信息
    * group Name文件的组名
    * remoteFileName:文件的存储路径名字
    * */

    public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {
        TrackerServer trackerServer = getTrackerServer();
        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //获取文件信息
        return storageClient.get_file_info(groupName,remoteFileName);

    }

    /*
    *
    * 删除文件
    * */
    public static void deleteFile(String groupName, String remoteFileName) throws Exception {
        TrackerServer trackerServer = getTrackerServer();
        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        storageClient.delete_file(groupName,remoteFileName);
    }

    /*
    *
    * 获取storage信息
    * */
    public static StorageServer getStorages() throws Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取storage信息
       return trackerClient.getStoreStorage(trackerServer);
    }



    /*
    * 文件下载
    * */
    public static InputStream downFile(String groupName, String remoteFileName) throws Exception {
        TrackerServer trackerServer = getTrackerServer();
        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //文件下载
        byte[] buffer = storageClient.download_file(groupName, remoteFileName);
        return new ByteArrayInputStream(buffer);
    }

    /***
     * 根据文件组名和文件存储路径获取Storage服务的IP、端口信息
     * @param groupName :组名
     * @param remoteFileName ：文件存储完整名
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName){
        try {
            //创建TrackerClient对象
            TrackerClient trackerClient = new TrackerClient();
            //通过TrackerClient获取TrackerServer对象
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取服务信息
            return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 获取Tracker服务地址
     */
    public static String getTrackerUrl() throws Exception {
        try {
            TrackerServer trackerServer = getTrackerServer();
            //获取Tracker地址
            return "http://"+trackerServer.getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static TrackerServer getTrackerServer() throws Exception {
        //创建TrackerClient对象
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient获取TrackerServer对象
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }


    public static void main(String[] args) throws Exception{
//        FileInfo fileInfo = getFile("group1", "M00/00/00/wKjThGBQD0eABGHvAANVzKhPd9M940.gif");
//        System.out.println(fileInfo.getSourceIpAddr());
//        System.out.println(fileInfo.getFileSize());

//        //文件下载
//        InputStream is = downFile("group1", "M00/00/00/wKjThGBQD0eABGHvAANVzKhPd9M940.gif");
//        //将文件写入本地磁盘
//        FileOutputStream os = new FileOutputStream("D:/1.gif");
//
//        //定义一个缓冲区
//        byte[] buffer = new byte[1024];
//        while (is.read(buffer)!=-1){
//            os.write(buffer);
//        }
//        os.flush();
//        os.close();
//        is.close();



        //文件删除
//        deleteFile("group1", "M00/00/00/wKjThGBQGOCAIwVfAAB22sDOFoU188.jpg");


//        StorageServer storageServer = getStorages();
//        System.out.println(storageServer.getStorePathIndex());


        //7
//        ServerInfo[] groups = getServerInfo("group1","M00/00/00/wKjThGBQG-KAKgN8AABHpZhEaPw971.jpg");
//        for (ServerInfo group : groups) {
//            System.out.println(group.getIpAddr());
//            System.out.println(group.getPort());
//        }

        //8
        System.out.println(getTrackerUrl());
    }
}
